package com.longxi.tianjintianbao.ui.presenter.maintain;

import android.content.Intent;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.listener.SelectMaintainListener;
import com.longxi.tianjintianbao.model.maintain.Maintain;
import com.longxi.tianjintianbao.ui.activity.maintain.MaintainDetailActivity;
import com.longxi.tianjintianbao.ui.activity.maintain.MaintainListActivity;
import com.longxi.tianjintianbao.ui.adapter.multitype.maintain.MaintainViewBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.base.LazyFragment;
import com.longxi.tianjintianbao.ui.view.maintain.IMaintainListFgView;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.longxi.tianjintianbao.utils.NListRefresh;

import org.json.JSONException;
import org.json.JSONObject;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MaintainListFgPresenter extends BasePresenter<IMaintainListFgView> {

    private MultiTypeAdapter adapter;
    private Items items = new Items();

    public MaintainListFgPresenter(BaseActivity context, LazyFragment fragment) {
        super(context, fragment);
    }

    public void initAdapter(String state){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            /* 注册类型和 View 的对应关系 */
            adapter.register(Maintain.class, new MaintainViewBinder(listener));
            getView().getRvContent().setAdapter(adapter);
        }
    }

    public void getEquipmentMaintainList(String state){
        JSONObject object = new JSONObject();
        try {
            object.put("userid", MyApplication.loginUser.userid);
            object.put("type", state);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().getEquipmentMaintainList(object)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(new MySubscriber<HttpResult<Maintain>>(mContext) {
            @Override
            public void onError(ExceptionHandle.ResponeThrowable throwable) {
                mContext.hideWaitingDialog();
                MyUtils.showToast(throwable.message);
            }

            @Override
            public void onNext(HttpResult<Maintain> result) {
                NListRefresh.getInstance().fjData(result,
                        getView().getRefreshLayout(),
                        items,
                        adapter,
                        mFragment);

                if("0".equals(state)) {
                    if(result.getData().size() > 0){
                        MaintainListActivity maintainListActivity = (MaintainListActivity) mContext;
                        maintainListActivity.setTabLayoutMsgCount(result.getData().size());
                    }
                }

            }
        });
    }

    SelectMaintainListener listener = new SelectMaintainListener() {
        @Override
        public void onSuccess(Maintain maintain) {
            Intent mainIntent = new Intent(mContext, MaintainDetailActivity.class);
            mainIntent.putExtra(AppConst.MAINTAIN, maintain);
            mContext.startActivityForResult(mainIntent, AppConst.IntentRequstCode.ACTIVITY_MAINTAIN_DETAIL);
        }
    };

    public void onDestroy() {
        listener = null;
    }
}
